package com.ling.pl.security.service;

import com.ling.pl.core.commons.support.DaoPage;
import com.ling.pl.core.commons.utils.BatchUtils;
import com.ling.pl.core.dao.IDao;
import com.ling.pl.security.model.DefaultRole;
import com.ling.pl.security.model.RoleMember;
import com.ling.pl.security.model.RoleResource;
import com.ling.pl.security.model.UrlComponent;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author bo.wang
 */
@Component(DefaultRoleService.BEAN_ID)
public class DefaultRoleService extends BaseDefaultRoleService {
    public List<DefaultRole> loadAllRoles() {
        return defaultRoleDao.loadAllRoles();
    }

    public List<RoleMember> loadRoleMemberByRoleId(String roleId) {
        return defaultRoleDao.loadRoleMemberByRoleId(roleId);
    }


    public IDao getDao() {
        return defaultRoleDao;
    }


    public List<RoleMember> loadRoleMemberByRoleNameAndType(String roleId, String type) {
        // TODO Auto-generated method stub
        return defaultRoleDao.loadRoleMemberByRoleNameAndType(roleId, type);
    }


    public DaoPage loadRoleMemberByRoleIdAndType(String roleId, String type,
                                                 Integer pageIndex, Integer pageSize) {
        // TODO Auto-generated method stub
        return defaultRoleDao.loadRoleMemberByRoleIdAndType(roleId, type, pageIndex, pageSize);
    }


    public Integer loadRoleMemberSizeByRoleIdAndType(String roleId, String type, String id) {
        return defaultRoleDao.loadRoleMemberSizeByRoleIdAndType(roleId, type, id);
    }


    public DaoPage loadAllRoles(Map params, Integer pageIndex, Integer pageSize) {
        return defaultRoleDao.loadAllRoles(params, pageIndex, pageSize);
    }


    public void saveRoles(Map dataListsMap) {
        Collection<DefaultRole> deleteList = BatchUtils.to(DefaultRole.class).getDeleteEntities(dataListsMap);
        Collection<DefaultRole> insertList = BatchUtils.to(DefaultRole.class).getInsertEntities(dataListsMap);
        Collection<DefaultRole> updateList = BatchUtils.to(DefaultRole.class).getModifiedEntities(dataListsMap);
        if ((updateList != null) && (updateList.size() > 0)) {
            for (DefaultRole role : updateList) {
                DefaultRole daorole = (DefaultRole) defaultRoleDao.get(DefaultRole.class, role.getId());
                daorole.setDesc(role.getDesc());
                daorole.setSortOrder(role.getSortOrder());
                defaultRoleDao.update(daorole);
            }
        }
        if ((insertList != null) && (insertList.size() > 0)) {
            for (DefaultRole role : insertList) {
                role.setId(UUID.randomUUID().toString());
//                role.setCompanyId(getFixedCompanyId());
                role.setCreateDate(new Date());
                role.setType(DefaultRole.NORMAL_TYPE);
                defaultRoleDao.save(role);
            }
        }
        if ((deleteList != null) && (deleteList.size() > 0)) {
            for (DefaultRole role : deleteList) {
                Map params = new HashMap();
                params.put("roleId", role.getId());
                List<RoleResource> roleResources = defaultRoleDao.loadRoleResourceByRoleId(role.getId());
                List<RoleMember> roleMembers = defaultRoleDao.loadRoleMemberByRoleId(role.getId());
                List<UrlComponent> roleUrlComponents = defaultRoleDao.loadUrlComponentByRoleId(role.getId());
                defaultRoleDao.removeAll(roleResources);
                defaultRoleDao.removeAll(roleMembers);
                defaultRoleDao.removeAll(roleUrlComponents);
                defaultRoleDao.remove(role);
            }
        }

    }


    public List<DefaultRole> loadUserRoles(String userName) {
        return defaultRoleDao.loadRolesByUserName(userName);
    }

}

